package com.zq.vaccinum.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zq.vaccinum.constant.Constants;
import com.zq.vaccinum.domain.JsonResult;
import com.zq.vaccinum.util.WebUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: zq
 * @Date: 2023/08/08/14:00
 * @Description:
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        JsonResult jsonResult = new JsonResult(Constants.FORBIDDEN,"授权未通过");
        String json = new ObjectMapper().writeValueAsString(jsonResult);
        WebUtils.renderString(response,json);
    }
}
